$(function () {
    $('#save-trade-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#trade-dialog');

        var start_time = $("input[name='start_time']").val();
        var end_time = $("input[name='end_time']").val();
        var submitType = self.attr("data-type");
        var trade_id = self.attr('data-id');


        zlajax.post({
            'url':'/cms/u_trade_time/',
            'data':{
                'start_time': start_time,
                'end_time': end_time,
                'trade_id': trade_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    zlalert.alertInfo(data['message']);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".update-trade-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#trade-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var start_time = tr.attr("data-start");
        var end_time = tr.attr("data-end");

        var startInput = $("input[name='start_time']");
        var endInput = $("input[name='end_time']");
        var savebtn = dialog.find("#save-trade-btn");

        startInput.val(start_time);
        endInput.val(end_time);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
})


$(function () {
    $('#save-withdraw-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#trade-dialog');

        // var withdraw_num = $("input[name='withdraw_num']").val();
        var gold_price = $("input[name='gold_price']").val();
        var submitType = self.attr("data-type");
        var trade_id = self.attr('data-id');


        zlajax.post({
            'url':'/cms/u_trade_withdraw/',
            'data':{
                // 'withdraw_num': withdraw_num,
                'gold_price': gold_price,
                'trade_id': trade_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    zlalert.alertInfo(data['message']);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".update-withdraw-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#withdraw-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var withdraw_num = tr.attr("data-withdraw");
        var gold_price = tr.attr("data-gold");


        // var withdrawInput = $("input[name='withdraw_num']");
        var goldInput = $("input[name='gold_price']");
        var savebtn = dialog.find("#save-withdraw-btn");

        // withdrawInput.val(withdraw_num);
        goldInput.val(gold_price);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.stop-trade-btn').click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var trade_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要暂停交易吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/reset_trade/',
                    'data':{
                        'trade_id':trade_id,
                        'status': 1
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            zlalert.alertError(data['message'])
                        }
                    }
                })
            }
        })
    })
});



$(function () {
    $('.start-trade-btn').click(function (event) {
        var self = $(this)
        var tr = self.parent().parent();
        var trade_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要开启交易吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/reset_trade/',
                    'data':{
                        'trade_id':trade_id,
                        'status': 0
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            zlalert.alertError(data['message'])
                        }
                    }
                })
            }
        })
    })
});